package com.shk.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.shk.annotation.Log4jAnnotation;
import com.shk.constant.MessageConstant;
import com.shk.entity.PageResult;
import com.shk.entity.QueryPageBean;
import com.shk.entity.Result;
import com.shk.pojo.Role;
import com.shk.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;



    /*添加*/
    @RequestMapping("/add")
    @Log4jAnnotation
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        roleService.add(role,permissionIds,menuIds);
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);

    }

    /*分页查询*/
    @RequestMapping("/findPage")
    @Log4jAnnotation
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult page = roleService.findPage(currentPage, pageSize, queryString);
        return page;
    }

    /*删除*/

    @RequestMapping("/delete")
    @Log4jAnnotation
    public Result delete(Integer id){
        roleService.delete(id);
        return new Result(true,MessageConstant.DELETE_ROLE_sUCCESS);
    }
        /*编辑表单数据回显*/
    @RequestMapping("/findPermissionIdsAndMenusIdsByRoleId")
    @Log4jAnnotation
    public Map findPermissionIdsByRoleId(Integer roleId){
       Map map =  roleService.findPermissionIdsAndMenusIdsByRoleId(roleId);
      return map;
    }

    /*编辑*/
    @RequestMapping("/edit")
    @Log4jAnnotation
    public Result update(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        roleService.update(role,permissionIds,menuIds);
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }

    @RequestMapping("/findAll")
    @Log4jAnnotation
    public Result findAll(){
       List<Role> data= roleService.findAll();
       return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,data);
    }

}
